package com.kaiyiweitong.tongxing.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kaiyiweitong.tongxing.R;

/**
 * Created by Administrator on 2017/12/21 0021.
 */

public class MyOrderAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private final LayoutInflater mInflater;
    private String[] mArr;
    private OnClickListener mListener;
    private int mCurrentSelect;

    public interface OnClickListener {
        void onClick(int position);
    }

    public MyOrderAdapter(Context context, String[] stringArray, OnClickListener listener, int select) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mArr = stringArray;
        mListener = listener;
        mCurrentSelect = select;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_my_order, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final MyViewHolder viewHolder = (MyViewHolder) holder;
        viewHolder.name.setText(mArr[position]);
        viewHolder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.onClick(position);
            }
        });
        if (mCurrentSelect == position) {
            viewHolder.name.setSelected(true);
            viewHolder.tab_line.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        if (mArr != null) {
            return mArr.length;
        } else {
            return 0;
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private View tab_line;

        public MyViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            tab_line = itemView.findViewById(R.id.tab_line);

        }
    }
}
